﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DataContext;
using Utils;
using Config;
using Entity;
using Library;

namespace BLL
{
    public class CtrRole
    {
         /// <summary>
        /// Thêm mới một Role
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int InsertRole(string name, string description, short status)
        {     
            name = HtmlUtility.HtmlEncode(name);
            description = HtmlUtility.HtmlEncode(description);
            return DB.AdminInstance.uspRoleInsert(name, description, status, AdminUtils.ServicesID);
        }

        /// <summary>
        /// Cập nhật thông tin chung của một Role
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public int UpdateRole(int roleID, string name, string description, short status)
        {            
            name = HtmlUtility.HtmlEncode(name);
            description = HtmlUtility.HtmlEncode(description);
            return DB.AdminInstance.uspRoleUpdate(roleID, name, description, status, AdminUtils.ServicesID);
        }

        /// <summary>
        /// Xóa một Role
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public int DeleteRole(int roleID)
        {            
            return DB.AdminInstance.uspRoleDelete(roleID);
        }

        /// <summary>
        /// Lấy danh sách Role
        /// </summary>
        /// <returns></returns>
        public ClassExtend<int, uspRoleGetListResult> GetListRole()
        {
            var objReturn = new ClassExtend<int, uspRoleGetListResult>
                                {
                                    Items =
                                        DB.AdminInstance.uspRoleGetList(AdminUtils.ServicesID).ToList()
                                };
            objReturn.TotalRecord = objReturn.Items.Count;
            objReturn.Info = 0;
            return objReturn;
        }

        /// <summary>
        /// Lấy thông tin chi tiết của một Role
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public uspRoleGetInfoResult GetRoleInfo(int roleID)
        {     
            return DB.AdminInstance.uspRoleGetInfo(roleID).ToList().FirstOrDefault();
        }

        /// <summary>
        /// Cập nhật Permission của một RoleFunction
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="functionID"></param>
        /// <param name="permission"> </param>
        /// <returns></returns>
        public int UpdateRoleFunction(int roleID, int functionID, int permission)
        {
            return DB.AdminInstance.uspRoleFunctionUpdate(roleID, functionID, permission);
        }
    }
}
